﻿package net.wangit.adm.role;

import net.wangit.MapList;
import net.wangit.context.ActionContext;
import net.wangit.jdbc.ColumnRow;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.Table;
import net.wangit.support.action.DefaultAction;
import net.wangit.user.admin.AdminCache;
import net.wangit.user.group.UserGroupCache;
import net.wangit.user.org.OrgCache;
import net.wangit.user.role.RoleCache;
import net.wangit.util.TreeUtil;

public class SaveRoleAction extends DefaultAction {

	private static final String ROLES_UPDATE = "update AROLE set parentid=? where parentid=?";
	private static final String ADMINISTRATOR_UPDATE = "update AADMINROLE set roleid=? where roleid=?";
	private static final String ORGANIZATION_UPDATE = "update AORGROLE set roleid=? where roleid=?";
	private static final String GROUP_UPDATE = "update AGROUPROLE set roleid=? where roleid=?";
	private static final String USER_UPDATE = "update AUSERROLE set roleid=? where roleid=?";
	private static final String PRIVILEGE_UPDATE = "update AROLEPRIVILEGE set roleid=? where roleid=?";
	private static final String LEVEL_QUERY = "select * from AROLE";
	private static final String LEVEL_UPDATE = "update AROLE set rolelevel=? where roleid=?";
	private static final int LEVEL_UPDATE_TYPES[] = { 11, 2 };

	public SaveRoleAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		super.doAction(db, ac);
		ColumnRow row = (ColumnRow) ((Table) ac.getTables().get(0)).getRows()
				.get(0);
		if (row.isUpdateRow()) {
			String newId = row.get("roleid");
			String oldId = row.getPrimaryKey("roleid");
			update(db, newId, oldId);
		}
		MapList orgs = db.query("select * from AROLE");
		updateLevel(db, orgs);
		if (row.isUpdateRow()) {
			RoleCache.clearAll();
			OrgCache.clearAll();
			UserGroupCache.clearAll();
			AdminCache.clearAll();
		}
	}

	private void update(DB db, String newRoleId, String oldRoleId)
			throws Exception {
		if (!oldRoleId.equals(newRoleId)) {
			String values[] = { newRoleId, oldRoleId };
			int types[] = { 2, 2 };
			db.execute("update AROLE set parentid=? where parentid=?", values,
					types);
			db.execute("update AADMINROLE set roleid=? where roleid=?", values,
					types);
			db.execute("update AORGROLE set roleid=? where roleid=?", values,
					types);
			db.execute("update AGROUPROLE set roleid=? where roleid=?", values,
					types);
			db.execute("update AUSERROLE set roleid=? where roleid=?", values,
					types);
			db.execute("update AROLEPRIVILEGE set roleid=? where roleid=?",
					values, types);
		}
	}

	private void updateLevel(DB db, MapList roles) throws Exception {
		for (int i = 0; i < roles.size(); i++) {
			String roleId = roles.getRow(i).get("roleid");
			MapList parents = TreeUtil.getParents(roles, roleId, "roleid",
					"parentid");
			db.execute("update AROLE set rolelevel=? where roleid=?",
					new String[] { Integer.toString(parents.size()), roleId },
					LEVEL_UPDATE_TYPES);
		}

	}

}
